/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.common.lang.validator;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;

/**
 * 
 *
 * @author Administrator
 * @version $Id: ValidatorUtil.java, v 0.1 2017-4-24 下午10:48:26 Administrator Exp $
 */
public class ValidatorUtil {

    public static void validate(Object object) {

        Errors error = new BeanPropertyBindingResult(new Object(), "object");
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.afterPropertiesSet();
        validator.validate(object, error);
        List<FieldError> el = error.getFieldErrors();
        if (CollectionUtils.isNotEmpty(el)) {
            FieldError fe = el.get(0);
            String msg = fe.getField() + " " + fe.getDefaultMessage();
            throw new ZpayException(BizCode.PARAM_ERROR, msg);
        }
    }
}
